package cn.iocoder.yudao.module.book.service.collect;


import cn.iocoder.yudao.module.book.controller.app.bookcase.vo.AppBooksRespVO;
import cn.iocoder.yudao.module.book.service.books.BooksService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.*;
import cn.iocoder.yudao.module.book.controller.admin.collect.vo.*;
import cn.iocoder.yudao.module.book.dal.dataobject.collect.CollectDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.book.dal.mysql.collect.CollectMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.book.enums.ErrorCodeConstants.*;

/**
 * 收藏 Service 实现类
 *
 * @author xi
 */
@Service
@Validated
public class CollectServiceImpl implements CollectService {

    @Resource
    private CollectMapper collectMapper;

    @Resource
    private BooksService booksService;

    @Override
    public Integer createCollect(CollectSaveReqVO createReqVO) {
        // 插入
        CollectDO collect = BeanUtils.toBean(createReqVO, CollectDO.class);
        collectMapper.insert(collect);
        // 返回
        return collect.getCollectionId();
    }

    @Override
    public List<CollectDO> listCollect(@Valid Long userId) {
//        查询所有
        return collectMapper.selectByUserId(userId);
    }

    @Override
    public void updateCollect(CollectSaveReqVO updateReqVO) {
        // 校验存在
        validateCollectExists(updateReqVO.getCollectionId());
        // 更新
        CollectDO updateObj = BeanUtils.toBean(updateReqVO, CollectDO.class);
        collectMapper.updateById(updateObj);
    }

    @Override
    public void deleteCollect(Integer id) {
        // 校验存在
        validateCollectExists(id);
        // 删除
        collectMapper.deleteById(id);
    }

    @Override
    public Boolean deleteCollectByBookId(Integer bookId, Long userId) {
//        通过bookId删除收藏的绘本
        return collectMapper.deleteByBookId(bookId,userId)>0;
    }

    private void validateCollectExists(Integer id) {
        if (collectMapper.selectById(id) == null) {
            throw exception(COLLECT_NOT_EXISTS);
        }
    }

    @Override
    public CollectDO getCollect(Integer id) {
        return collectMapper.selectById(id);
    }

    @Override
    public PageResult<CollectDO> getCollectPage(CollectPageReqVO pageReqVO) {
        return collectMapper.selectPage(pageReqVO);
    }

    @Override
    public CollectDO CollectByBookId(Integer bookId,Integer userId) {
//        根据bookId查询
        return collectMapper.selectByBookId(bookId,userId);
    }

    @Override
    public Boolean collect(Integer bookId, Long userId) {
        if(collectMapper.existsByBookId(bookId,userId)>0){
            throw exception(COLLECT_EXISTS);
        }
        AppBooksRespVO book=booksService.getByBookId(bookId,null);

//        return collectMapper.insert((CollectDO) new CollectDO().setCollectionId(book.getCollectionId())
//                .setBookId(bookId)
//                .setUserId(userId)
//                .setCreateTime(LocalDateTime.now()))>0;
        String createTime = String.valueOf(LocalDateTime.now());
        return collectMapper.insertCollect(bookId,userId,book.getCollectionId(), createTime)>0;
    }

    @Override
    public List<CollectDO> getCollectByUserId(Long currentUserId) {
        return collectMapper.selectByUserId(currentUserId);
    }

    @Override
    public List<CollectDO> listCollectByType(Long userId, String type) {
        return collectMapper.selectByUserIdAndType(userId, type);
    }

    @Override
    public Boolean deleteCollectByCollectionId(Integer collectionId, Long userId) {
        return collectMapper.deleteByCollectionId(collectionId, userId) > 0;
    }

    @Override
    public Boolean collectCollection(Integer collectionId, Long userId) {
        if(collectMapper.existsByCollectionId(collectionId, userId) > 0){
            throw exception(COLLECT_EXISTS);
        }
        String createTime = String.valueOf(LocalDateTime.now());
        return collectMapper.insertCollectCollection(collectionId, userId, createTime) > 0;
    }

    @Override
    public Integer cleanDuplicateCollects(Long userId) {
        return collectMapper.cleanDuplicatesByUserId(userId);
    }

    @Override
    public boolean existsCollection(Integer collectionId, Long userId) {
        try {
            return collectMapper.existsByCollectionId(collectionId, userId) > 0;
        } catch (Exception ignore) {
            return false;
        }
    }
}